package com.gpframework.module.controller.ctn;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.ctn.domain.GpCtnOrder;
import com.gpframework.module.ctn.domain.req.GpCtnOrderAdd;
import com.gpframework.module.ctn.domain.req.GpCtnOrderReq;
import com.gpframework.module.ctn.domain.req.GpCtnOrderUpdate;
import com.gpframework.module.ctn.domain.vo.GpCtnOrderVo;
import com.gpframework.module.ctn.service.IGpCtnLoadedService;
import com.gpframework.module.ctn.service.IGpCtnOrderService;
import com.gpframework.module.ctn.service.IGpCtnReachPortService;
import com.gpframework.module.ctn.service.IGpTelexReleaseService;
import com.gpframework.module.module.domain.GpAgentCompany;
import com.gpframework.module.module.service.IGpAgentCompanyService;
import com.gpframework.module.module.service.IGpDomesticFreightForwardingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpCtnOrderController
 * @Author gpframework
 * @Date 2024-01-08
 * @Version 1.0.0
 * @Description 定货柜填写Controller 接口类
 */
@Validated
@Api(tags = "定货柜填写")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/ctn/order" )
public class GpCtnOrderController extends BaseController {

    private final IGpCtnOrderService iGpCtnOrderService;
    private final IGpCtnLoadedService iGpCtnLoadedService;
    private final IGpAgentCompanyService iGpAgentCompanyService;
    private final IGpCtnReachPortService iGpCtnReachPortService;
    private final IGpTelexReleaseService iGpTelexReleaseService;
    private final IGpDomesticFreightForwardingService iGpDomesticFreightForwardingService;


 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-08
  * @Param gpCtnOrder
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @GetMapping("/list")
    public TableDataInfo<GpCtnOrderVo> list(GpCtnOrderReq gpCtnOrderReq)
    {
        startPage();
        QueryWrapper<GpCtnOrder> lqw = new QueryWrapper<GpCtnOrder>();
        gpCtnOrderReq.generatorQuery(lqw,true);
        List<GpCtnOrderVo> list = iGpCtnOrderService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnOrder
     * @Return AjaxResult
     */
    @ApiOperation("详情")
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpCtnOrderVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpCtnOrderService.getVoById(id));
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnOrder
     * @Return AjaxResult
     */
    @ApiOperation("新增")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpCtnOrderAdd gpCtnOrderAdd) {
        GpAgentCompany agentCompany = iGpAgentCompanyService.getById(gpCtnOrderAdd.getAgentCompanyId());
        GpCtnOrder ctnOrder = iGpCtnOrderService.getOne(new QueryWrapper<GpCtnOrder>().eq("agent_ctn_no", gpCtnOrderAdd.getAgentCtnNo()).eq("del_status", YesNoEnum.NO.getKey()));
        if (ctnOrder!=null){
            return AjaxResult.error("货柜序货柜序号重复");
        }
        if (gpCtnOrderAdd.getAgentCompanyId()==null){
            return AjaxResult.error("agentCompanyId is null");
        }
        if (agentCompany ==null){
            return AjaxResult.error("agentCompany is null");
        }
        if (StringUtils.equals(gpCtnOrderAdd.getAgentCompany(), agentCompany.getAgentCompany())){
            return AjaxResult.error("请求的清关公司名在数据库中不存在");
        }
        GpCtnOrder gpCtnOrder = Convert.convert(new TypeReference<GpCtnOrder>() {}, gpCtnOrderAdd);
        return toAjax(iGpCtnOrderService.save(gpCtnOrder) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnOrder
     * @Return AjaxResult
     */
    @ApiOperation("修改")
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpCtnOrderUpdate gpCtnOrderUpdate) {
        GpCtnOrder ctnOrder = iGpCtnOrderService.getOne(new QueryWrapper<GpCtnOrder>().eq("agent_ctn_no", gpCtnOrderUpdate.getAgentCtnNo()).eq("del_status", YesNoEnum.NO.getKey()));
        if (ctnOrder!=null){
            return AjaxResult.error("货柜序货柜序号重复");
        }
        GpCtnOrder gpCtnOrder = Convert.convert(new TypeReference<GpCtnOrder>() {}, gpCtnOrderUpdate);
        return toAjax(iGpCtnOrderService.updateById(gpCtnOrder) ? 1 : 0);
    }

    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-08
     * @Param gpCtnOrder
     * @Return AjaxResult
     */
    @ApiOperation("删除")
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpCtnOrderService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
